//
//  CustomCategoryViewController.m
//  iShop
//
//  Created by svp on 17/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomCategoryViewController.h"
#import "CustomProductViewController.h"
#import "HobbiesViewController.h"
#import "SearchViewController.h"
#import "UIImageEx.h"
#import "ResourceLoader.h"
#import "CoreDataUtils.h"
#import "Utils.h"
#import "ConstansDefine.h"

@implementation CustomCategoryViewController

@synthesize tableView = _tableView;
@synthesize shop = _shop;
@synthesize hobbiesButton = _hobbiesButton;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _customGroup = [[NSMutableArray alloc] init];
    }
    return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	self.title = @"Group Category";
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
	backButton.title = @"Back";
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];
	if (_customGroup.count > 0) {
		[_customGroup removeAllObjects];
	}
	[_customGroup addObjectsFromArray:[CoreDataUtils getRootCategoryFromShop:_shop]];
	[_tableView reloadData];
	
	NSObject *ksession = [Utils getValueForRefKey:kSessionKey];
	if (!ksession || ksession == [NSNull null]) {
		_hobbiesButton.enabled = NO;
	}
	else {
		_hobbiesButton.enabled= YES;
	}
}

- (void)dealloc {	
	NSLog(@"CustomCategoryViewController  dealloc");
	[_tableView release];
	[_customGroup release];
    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 3;//_customGroup.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomGroupCell"];
	if (!cell) {
		NSLog(@"Create cell");
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CustomGroupCell"] autorelease];
	}
	Category *category = [_customGroup objectAtIndex:indexPath.row];
	cell.imageView.image = [[[Utils getImageOf:@"category" withName:category.CategoryImage] changeWhiteColorTransparent] scaleToSize:CGSizeMake(70, 70)];
	cell.textLabel.text = category.CategoryName;
	cell.detailTextLabel.text = [NSString stringWithFormat:@"Has %i product(s)",[CoreDataUtils countProductOnShop:_shop andCategory:category]];
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSLog(@"didSelectRowAtIndexPath:%@",indexPath);
	CustomProductViewController *custom = [[CustomProductViewController alloc] initWithNibName:@"CustomProductViewController" bundle: nil];
	custom.shop = _shop;
	custom.rootCategory = [_customGroup objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:custom animated:YES];
	[custom release];
}

-(IBAction) openHobbies:(id) sender
{
	NSLog(@"openHobbies");
	HobbiesViewController *custom = [[HobbiesViewController alloc] initWithNibName:@"HobbiesViewController" bundle: nil];
	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:custom];
	[self.navigationController presentModalViewController:nav animated:YES];
	[custom release];
	[nav release];
}

-(IBAction) openSearchScreen:(id) sender
{
	NSLog(@"openSearchScreen");
	SearchViewController *custom = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle: nil];
	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:custom];
	[self.navigationController presentModalViewController:nav animated:NO];
	//[self.navigationController pushViewController:custom animated:YES];
	[custom release];
	[nav release];
}


@end
